<template>
  <ele-page>
    <!-- 搜索表单 -->
    <article-search @search="reload" :typeOptions="typeOptions"/>
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        cache-key="cmsArticleTable"
      >
        <template #toolbar>
          <el-button
            type="primary"
            class="ele-btn-icon"
            :icon="PlusOutlined"
            @click="openAdd()"
          >
            新建
          </el-button>
          <el-button
            type="danger"
            class="ele-btn-icon"
            :icon="DeleteOutlined"
            @click="removeBatch()"
          >
            删除
          </el-button>
<!--          <el-button class="ele-btn-icon" :icon="DownloadOutlined" @click="exportData">-->
<!--            导出-->
<!--          </el-button>-->
        </template>
        <template #action="{ row }">
          <el-link type="primary" :underline="false" @click="openEdit(row)">
            修改
          </el-link>
          <el-divider direction="vertical"/>
          <el-link type="danger" :underline="false" @click="removeBatch(row)">
            删除
          </el-link>
        </template>
        <template #top="{row}">
          <span v-if="row.top === '0'">
            ×
          </span>
          <span v-else>
            √
          </span>
        </template>
        <template #views="{row}">
          <span>{{ row.views }}</span>
        </template>
        <template #status="{row}">
          <el-tag v-if="row.status === '0'" type="warning">暂存</el-tag>
          <el-tag v-else type="success">发布</el-tag>
        </template>
        <template #typeList="{row}">
          <el-tag v-for="(item,index) in row.types" :key="index" type="info" effect="plain">{{ item.typeName }}</el-tag>
        </template>
        <template #articlePic="{row}">
          <el-image
            v-if="row.articlePicType === '0'"
            style="width: 120px;height: 60px;"
            :src="row.articlePicLink"
            lazy
            :preview-src-list="[row.articlePicLink]" :preview-teleported="true">
            <template #error>
              <div class="no-image-slot">
                <el-icon>
                  <icon-picture/>
                </el-icon>
              </div>
            </template>
          </el-image>
          <el-image
            v-if="row.articlePicType === '1'"
            style="width: 120px;height: 60px;"
            :src="row.articlePic"
            lazy
            :preview-src-list="[row.articlePic]" :preview-teleported="true">
          </el-image>
        </template>
      </ele-pro-table>
    </ele-card>
    <article-add v-model="showAdd" :typeOptions="typeOptions" @done="reload"/>
    <!-- 编辑弹窗 -->
    <article-edit v-model="showEdit" :typeOptions="typeOptions" :data="current" @done="reload"/>
  </ele-page>
</template>

<script setup>
import {onMounted, ref} from 'vue';
import {
  PlusOutlined,
  DeleteOutlined,
  DownloadOutlined
} from '@/components/icons';
import {ElMessageBox} from 'element-plus/es';
import {EleMessage} from 'ele-admin-plus/es';
import ArticleSearch from './components/article-search.vue';
import ArticleAdd from './components/article-add.vue';
import ArticleEdit from './components/article-edit.vue';
import {pageArticles, removeArticles, exportArticles} from '@/api/cms/article';
import {dropDownListTypes} from '@/api/cms/type';
import {Picture as IconPicture} from '@element-plus/icons-vue';

/** 表格实例 */
const tableRef = ref(null);

/** 表格列配置 */
const columns = ref([
  {
    type: 'selection',
    columnKey: 'selection',
    width: 50,
    align: 'center',
    fixed: 'left'
  },
  {
    prop: 'articlePic',
    label: '首图预览',
    align: 'center',
    slot: 'articlePic'
  },
  {
    prop: 'title',
    label: '标题',
    align: 'center',
    miniWidth: 200
  },
  {
    prop: 'typeList',
    label: '分类',
    align: 'center',
    slot: 'typeList'
  },
  {
    prop: 'top',
    label: '置顶',
    align: 'center',
    slot: 'top'
  },
  {
    prop: 'views',
    label: '阅读量',
    align: 'center',
    slot: 'views'
  },
  {
    prop: 'status',
    label: '状态',
    align: 'center',
    slot: 'status'
  },
  {
    columnKey: 'action',
    label: '操作',
    width: 180,
    align: 'center',
    slot: 'action'
  }
]);

/** 表格选中数据 */
const selections = ref([]);

/** 当前编辑数据 */
const current = ref(null);


/** 是否显示新增弹窗 */
const showAdd = ref(false);

/** 是否显示编辑弹窗 */
const showEdit = ref(false);

/**
 * 文章分类选项
 * @type {Ref<UnwrapRef<*[]>>}
 */
const typeOptions = ref([]);

/** 表格数据源 */
const datasource = ({pages, where}) => {
  return pageArticles({...where, ...pages});
};

/** 搜索 */
const reload = (where) => {
  tableRef.value?.reload?.({page: 1, where});
};

/** 打开新增弹窗 */
const openAdd = () => {
  showAdd.value = true;
};


/** 打开编辑弹窗 */
const openEdit = (row) => {
  current.value = row ?? null;
  showEdit.value = true;
};

/** 批量删除 */
const removeBatch = (row) => {
  const rows = row == null ? selections.value : [row];
  if (!rows.length) {
    EleMessage.error('请至少选择一条数据');
    return;
  }
  ElMessageBox.confirm(
    `是否确认删除标题为"${rows.map((d) => d.title).join()}"的数据项?`,
    '系统提示',
    {type: 'warning', draggable: true}
  )
    .then(() => {
      const loading = EleMessage.loading('请求中..');
      removeArticles(rows.map((d) => d.id))
        .then(() => {
          loading.close();
          EleMessage.success('删除成功');
          reload();
        })
        .catch((e) => {
          loading.close();
          EleMessage.error(e.message);
        });
    })
    .catch(() => {
    });
};

/** 导出数据 */
// const exportData = () => {
//   const loading = EleMessage.loading('请求中..');
//   tableRef.value?.fetch?.(({where}) => {
//     exportArticles(where)
//       .then(() => {
//         loading.close();
//       })
//       .catch((e) => {
//         loading.close();
//         EleMessage.error(e.message);
//       });
//   });
// };


onMounted(() => {
  dropDownListTypes().then(res => {
    console.log(res.data);
    typeOptions.value = res.data;
  });
});
</script>


<style scoped lang="scss">
.el-tag + .el-tag {
  margin-left: 10px;
}

.el-table .warning-row {
  background: #f8f8f9;
}

.articleFilesInfoName {
  text-align: center;
  padding-top: 5px;
}

.tabBlock {
  height: 180px;
  margin-top: 20px;
}

.articlePic {
  width: 200px;
  height: 100px;
}

.no-image-slot {
  color: #909399;
  font-size: 28px;
  background-color: rgb(244, 244, 245);
  padding-top: 15px;
  height: 60px;
}
</style>
